import java.util.*;

// 学生类
class Student {
    private String name;
    private int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    @Override
    public String toString() {
        return name + "，年龄：" + age;
    }
}

// 迭代器接口
interface Iterator {
    boolean hasNext();
    Student next();
}

// 迭代器实现：数组迭代器
class ArrayIterator implements Iterator {
    private Student[] students;
    private int index = 0;

    public ArrayIterator(Student[] students) {
        this.students = students;
    }

    @Override
    public boolean hasNext() {
        return index < students.length;
    }

    @Override
    public Student next() {
        return students[index++];
    }
}

// 迭代器实现：列表迭代器
class ListIterator implements Iterator {
    private List<Student> students;
    private int index = 0;

    public ListIterator(List<Student> students) {
        this.students = students;
    }

    @Override
    public boolean hasNext() {
        return index < students.size();
    }

    @Override
    public Student next() {
        return students.get(index++);
    }
}

// 班级接口
interface Class {
    Iterator createIterator();
}

// 数组班级类
class ArrayClass implements Class {
    private Student[] students;

    public ArrayClass(Student[] students) {
        this.students = students;
    }

    @Override
    public Iterator createIterator() {
        return new ArrayIterator(students);
    }
}

// 列表班级类
class ListClass implements Class {
    private List<Student> students;

    public ListClass(List<Student> students) {
        this.students = students;
    }

    @Override
    public Iterator createIterator() {
        return new ListIterator(students);
    }
}

// 客户端：教务管理系统
public class SchoolSystem {
    public static void main(String[] args) {
        // 创建学生对象
        Student s1 = new Student("张三", 18);
        Student s2 = new Student("李四", 21);
        Student s3 = new Student("王五", 19);

        // 创建班级并添加学生
        Class class1 = new ArrayClass(new Student[]{s1, s2, s3});
        Class class2 = new ListClass(Arrays.asList(s1, s2, s3));

        // 按年龄排序
        List<Student> sortedList = new ArrayList<>();
        Iterator iterator1 = class1.createIterator();
        while (iterator1.hasNext()) {
            sortedList.add(iterator1.next());
        }

        sortedList.sort(Comparator.comparingInt(Student::getAge));

        // 输出学生信息
        for (Student student : sortedList) {
            System.out.println(student);
        }
    }
}
